package com.catazy.module_project.repository

import android.util.Log
import com.catazy.module_project.bean.ArticleDetail
import com.catazy.module_project.bean.ArticleDetailWithTag
import com.catazy.module_project.bean.ProjectBean
import com.catazy.module_project.bean.Tag
import com.catazy.module_project.local.AppDatabase
import com.catazy.module_project.local.DBUtil
import com.catazy.module_project.local.dao.ArticleDetailDao
import com.catazy.module_project.local.dao.ProjectTreeDao
import com.catazy.module_project.local.dao.TagDao
import com.catazy.wanandroid.common_base.net.download.DownloadListener
import com.catazy.wanandroid.common_base.net.download.FileDownloader
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 * 作者: Tao
 * 日期: 2024/5/7
 * 时间: 9:57
 * 描述: 项目本地数据仓库
 */
@ActivityRetainedScoped
class ProjectLocalRepository @Inject constructor() {

    companion object {
        private const val TAG = "ProjectLocalRepository"
    }

    /**
     * 插入项目分类数据
     */
    fun insertProjectTree(projectBeans: List<ProjectBean>) {
        DBUtil.getInstance().runInTransaction {
            projectBeans.forEach {
                DBUtil.getInstance().projectTreeDao().insert(it)
            }
        }
    }

    /**
     * 获取项目分类数据
     */
    fun getProjectTree(): Flow<List<ProjectBean>> {
        return DBUtil.getInstance().projectTreeDao().getProjectTree()
    }

    /**
     * 插入项目文章数据
     */
    fun insertProjectArticle(articleDetails: List<ArticleDetail>) {
        DBUtil.getInstance().runInTransaction {
            articleDetails.forEach { articleDetail ->
                DBUtil.getInstance().articleDetailDao().insert(articleDetail)
                articleDetail.tags?.forEach {
                    it.artileId = articleDetail.id
                    insertTag(it)
                }
            }
        }
    }

    /**
     * 获取项目文章数据
     */
    fun getProjectArticles(): Flow<MutableList<ArticleDetail>> {
        return DBUtil.getInstance().articleDetailDao().getArticleDetailList()
    }

    /**
     * 插入标签
     */
    fun insertTag(tag: Tag) {
        DBUtil.getInstance().tagDao().insert(tag)
    }

    /**
     * 获取标签
     */
    fun getTags(): Flow<List<Tag>> {
        return DBUtil.getInstance().tagDao().getTagList()
    }

    /**
     * 获取文章详情
     */
    fun getArticleDetailWithTag(): Flow<MutableList<ArticleDetailWithTag>> {
        return DBUtil.getInstance().articleDetailDao().getArticleDetailWithTag()
    }

    /**
     * 删除文章
     */
    fun deleteArticleById(articleId: String) {
        DBUtil.getInstance().articleDetailDao().deleteArticleById(articleId)
    }

    /**
     * 下载文件
     */
    suspend fun downloadFile(url: String, path: String) {
        FileDownloader.create(url)
            .setPath(path)
            .setListener(object : DownloadListener {
                override fun onStart() {
                    Log.i(TAG, "下载开始: ")
                }

                override fun onProgress(progress: Int, total: Float) {
                    Log.i(TAG, "下载进度: $progress")
                }

                override fun onFinish(path: String, url: String) {
                    Log.i(TAG, "下载完成: ")
                    DBUtil.getInstance().articleDetailDao().updatePathByUrl(path, url)
                }

                override fun onFail(msg: String?) {
                    Log.i(TAG, "下载失败: $msg")
                }
            })
            .start()
    }
}